import oss2

# 配置信息
auth = oss2.Auth('LTAI5tAWzJupucXUMqqt6vYL', 'BBwZUOslSVT6L1Gp48OhjXVxp8opTr')
bucket = oss2.Bucket(auth, 'oss-cn-hangzhou.aliyuncs.com', 'qinxiao')


# 上传文件
def upload_file_to_oss(file_path, object_name):
    try:
        # 上传文件
        bucket.put_object_from_file(object_name, file_path)
        print(f"文件 {file_path} 上传成功，保存为 {object_name}")

        # 获取文件URL（公开读情况下）
        url = bucket.sign_url('GET', object_name, 3600)  # 1小时有效期的URL
        print(f"文件访问URL: {url}")
        return url
    except Exception as e:
        print(f"上传失败: {e}")
        return None


# 使用示例
# upload_file_to_oss('../uploads/uploaded-image.png', 'images/uploaded_image.jpg')
